package com.fzhucc.modules.businessCard.rest;


import com.fzhucc.modules.businessCard.domain.dto.BusinessCardAddDto;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardEditDto;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardQRCodeDto;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardQueryCriteria;
import com.fzhucc.modules.businessCard.service.BusinessCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yyg
 * @date 2022-10-24
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "名片：管理")
@RequestMapping("/api/businessCard")
public class BusinessCardController {

    private final BusinessCardService businessCardService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('businessCard:list')")
    public void exportBusinessCard(HttpServletResponse response, BusinessCardQueryCriteria criteria) throws IOException {
        businessCardService.download(businessCardService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询businessCard")
    @ApiOperation("查询businessCard")
    //@PreAuthorize("@el.check('businessCard:list')")
    public ResponseEntity<Object> queryBusinessCard(BusinessCardQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(businessCardService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增businessCard")
    @ApiOperation("新增businessCard")
    //@PreAuthorize("@el.check('businessCard:add')")
    public ResponseEntity<Object> createBusinessCard(@Validated @RequestBody BusinessCardAddDto resources) {
        return new ResponseEntity<>(businessCardService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改businessCard")
    @ApiOperation("修改businessCard")
    //@PreAuthorize("@el.check('businessCard:edit')")
    public ResponseEntity<Object> updateBusinessCard(@Validated @RequestBody BusinessCardEditDto resources) {
        businessCardService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除businessCard")
    @ApiOperation("删除businessCard")
    @PreAuthorize("@el.check('businessCard:del')")
    public ResponseEntity<Object> deleteBusinessCard(@RequestBody Integer[] ids) {
        businessCardService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("上传名片头像")
    @PostMapping(value = "/uploadPhoto")
    @AnonymousAccess
    public ResponseEntity<Object> uploadPhoto(@RequestParam MultipartFile photo,@RequestParam Integer id){
        return new ResponseEntity<>(businessCardService.douploadpic(photo,id), HttpStatus.OK);
    }

    @PostMapping(value = "/getQRcode")
    @Log("生成二维码")
    @ApiOperation("生成二维码")
    public ResponseEntity<Object> getQRcode(@Validated @RequestBody BusinessCardQRCodeDto businessCardQRCodeDto) {
        return new ResponseEntity<>(businessCardService.makeQRcode(businessCardQRCodeDto.getId()), HttpStatus.OK);
    }


    @Log("根据手机号查询名片库,同步名片功能")
    @ApiOperation("根据手机号查询名片库,同步名片功能")
    @PostMapping("/synchronizationExpert")
    public ResponseEntity<Object> getsynchronizationExpert(){
        return new ResponseEntity<>(businessCardService.dosynchronizationExpert(), HttpStatus.OK);
    }

}